const express = require('express');
//创建路由对象
const bus = express.Router();
//引入数据库
const pool = require('../pool/pool');

/*******************************************/
//1.通过id查询某个商家接口(get /)

//请求方式：get
bus.get('/select/:b_id', (req, res, next) => {
  //4.1 获取get传递的参数
  console.log(req.params);
  var obj = req.params;
  //4.2 执行SQL命令
  pool.query('SELECT * FROM business WHERE b_id=?', [obj.b_id], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    console.log(result);
    if (result.length === 0) {
      res.send({ code: 201, msg: '查询失败,不存在商家' });
    } else {
      res.send({ code: 200, msg: '查询成功', data: result });
    }
  });
});
//2.查询所有商家接口(post /)
bus.post('/select', (req, res, next) => {
  let obj = req.body
  let pageNum = obj.pageNum
  let pageSize = obj.pageSize
  //4.2 执行SQL命令，从数据表business 分页 查询数据
  let sql = `select * from business limit ${pageNum},${pageSize}`
  pool.query(sql, [pageNum, pageSize], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    console.log(result);
    if (result.length === 0) {
      res.send({ code: 201, msg: '查询失败,不存在商家', data: result });
    } else {
      res.send({ code: 200, msg: '查询成功', data: result });
    }
  });
});



/*******************************************/
//导出
module.exports = bus;
